package controller

import (
	"ez/apps/mes/auto/controller"
	mes "ez/apps/mes/document"
	"ez/custom/cc"
	"gitee.com/dreamwood/ez-go/ez"
)

type PaperController struct {
	controller.PaperAutoController
}

func init() {
	c := &PaperController{}
	c.SetRouteParam("/mes", "/admin", "Paper")
	ez.CreateApi(c.AdminGet(), c.GetAction).SetRouterName("[mes]报工单_创建")
	ez.CreateApi(c.AdminList(), c.ListAction).SetRouterName("[mes]报工单_列表")
	ez.CreateApi(c.AdminSave(), c.SaveAction).SetRouterName("[mes]报工单_保存")
	ez.CreateApi(c.AdminCopy(), c.CopyAction).SetRouterName("[mes]报工单_复制")
	ez.CreateApi(c.AdminUpdate(), c.UpdateAction).SetRouterName("[mes]报工单_更新")
	ez.CreateApi(c.AdminChoice(), c.ChoiceAction).SetRouterName("[mes]报工单_选项")
	ez.CreateApi(c.AdminDelete(), c.DeleteAction).SetRouterName("[mes]报工单_删除")
	ez.CreateApi(c.AdminUnDelete(), c.UnDeleteAction).SetRouterName("[mes]报工单_恢复")
	ez.CreateApi(c.AdminDestroy(), c.DestroyAction).SetRouterName("[mes]报工单_销毁")
	ez.CreateApi(c.AdminUpdateMany(), c.UpdateManyAction).SetRouterName("[mes]报工单_更新_批量")
	ez.CreateApi(c.AdminDeleteMany(), c.DeleteManyAction).SetRouterName("[mes]报工单_删除_批量")
	ez.CreateApi(c.AdminDestroyMany(), c.DestroyManyAction).SetRouterName("[mes]报工单_销毁_批量")
	ez.CreateApi(c.AdminModelApi("save_manuel"), c.SaveManuelAction).SetRouterName("[mes]报工单_报工保存")
}

// func (c PaperController) AccessControl(session *ez.Session) { /* 在这里面重构 */ }
// func (c PaperController) GetAction(session *ez.Session) { /* 在这里面重构 */ }
// func (c PaperController) ListAction(session *ez.Session) { /* 在这里面重构 */ }
func (c PaperController) SaveManuelAction(session *ez.Session) {
	this := cc.New(session)
	paper := new(mes.Paper)
	if this.Try(this.FillJson(paper)) {
		return
	}
	//提取出关联数据
	srcPapers := paper.SrcPapers
	paper.SrcPapers = nil
	if this.Try(paper.Save()) {
		return
	}
	if srcPapers == nil {
		this.ReturnSuccess("OK", "保存成功")
		return
	}
	//设置关联关系
	for _, srcPaper := range srcPapers {
		srcPaper.DestPaperId = paper.Id
		srcPaper.SetEvent(true)
		_ = srcPaper.Save()
	}
	this.ReturnSuccess("OK", "保存成功")
}
